﻿using System;
using System.Windows.Forms;
using System.Xml;

using ClassLibrary;
using Core;

namespace TestProject
{
    public partial class NewVariant : Form
    {
        private Model model;
        private NaturalSelectionTable natsel_t;
        private int id;
        NaturalSelectionLink natsel_l = null;
        public NewVariant(Model m, NaturalSelectionTable t)
        {
            InitializeComponent();
            natsel_t = t;
            id = Functions.set_id(t);
            model = m;
        }

        private void ok_butt_Click(object sender, EventArgs e)
        {
            if ((natsel_link_box.SelectedItem == null)||(reprod_link_box.SelectedItem==null))
            {
                MessageBox.Show("Please select links.");
                return;
            }
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(natsel_t.path));
            XmlElement el = doc.CreateElement("natural_selection_variant");
            el.SetAttribute("Id", id.ToString());
            el.SetAttribute("FirstTimeStep", first_step.Value.ToString());
            el.SetAttribute("LastTimeStep", last_step.Value.ToString());
            el.SetAttribute("AmountIndividualsLowerBound", low_border.Value.ToString());
            el.SetAttribute("AmountIndividualsUpperBound", upper_border.Value.ToString());
            foreach (NaturalSelectionLink link in model.model_natural_selection_links.Values)
            {
                if (link.name == (string)natsel_link_box.SelectedItem)
                {
                    natsel_l = link;
                    break;
                }
            }
            ReproductionLink reprod_l = null;
            foreach (ReproductionLink link in model.model_reproduction_links.Values)
            {
                if (link.name == (string)reprod_link_box.SelectedItem)
                {
                    reprod_l = link;
                    break;
                }
            }
            el.SetAttribute("PathToNaturalSelectionLink", natsel_l.path);
            el.SetAttribute("PathToReproductionLink", reprod_l.path);
            doc.DocumentElement.AppendChild(el);
            doc.Increment(doc.DocumentElement.Attributes[1]);
            doc.Save(natsel_t.path);
            this.Close();
        }

        private void NewVariant_Load(object sender, EventArgs e)
        {
            foreach (NaturalSelectionLink link in model.model_natural_selection_links.Values)
            {
                natsel_link_box.Items.Add(link.name);
            }
            
        }

        private void natsel_link_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            reprod_link_box.Enabled = true;
            foreach (NaturalSelectionLink link in model.model_natural_selection_links.Values)
            {
                if (link.name == (string)natsel_link_box.SelectedItem)
                {
                    natsel_l = link;
                    break;
                }
            }
            foreach (ReproductionLink link in model.model_reproduction_links.Values)
            {
                if(link.path_to_natsel_link==natsel_l.path)
                reprod_link_box.Items.Add(link.name);
            }
            reprod_link_box.SelectedIndex = 0;
        }

    }
}
